﻿using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.DrawingStandard
{
    public class TextPloter
    {
        public enum eTxtLocation
        {
            E_LEFT,
            E_RIGHT,
            E_TOP,
            E_BOTTOM
        }
      
        public static DBObjectCollection PlotText(Database db, ref Extents2d ext, eTxtLocation eLocation, Point2d Point, string text, double _scale, string tstyle = "SRB.HT", double Rotation = 0)
        {
            tstyle = "SRB.HT";
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;

                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //DBText
                MText Mtext = new MText();
                Mtext.TextHeight = 2.5 * _scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                //Mtext.Width = 200 * _scale;
                Mtext.Rotation = Rotation;
                res.Add(Mtext);
                switch (eLocation)
                {
                    case eTxtLocation.E_LEFT:
                        {
                            Mtext.Location = Point.C3D(-Mtext.ActualHeight);
                            Mtext.Attachment = AttachmentPoint.TopCenter;
                            break;
                        }
                    case eTxtLocation.E_RIGHT:
                        {
                            Mtext.Location = Point.C3D(Mtext.ActualHeight);
                            Mtext.Attachment = AttachmentPoint.BottomCenter;
                            break;
                        }
                    case eTxtLocation.E_TOP:
                        {
                            Mtext.Location = Point.C3D(0, Mtext.ActualHeight);
                            Mtext.Attachment = AttachmentPoint.TopCenter;
                            break;
                        }
                    case eTxtLocation.E_BOTTOM:
                        {
                            Mtext.Location = Point.C3D(0, -Mtext.ActualHeight);
                            Mtext.Attachment = AttachmentPoint.BottomCenter;
                            break;
                        }
                    default:
                        break;
                }

                recorder.AppendEntity(Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);
                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
                return res;
            }
        }
        /// <summary>
        /// 带比例尺标题
        /// </summary>
        /// <param name="db"></param>
        /// <param name="Point">插入点坐标</param>
        /// <param name="text">标题文本</param>
        /// <param name="scaleString">比例尺文本</param>
        /// <param name="ext"></param>
        /// <param name="titleFontSize">标题文本大小</param>
        /// <param name="scaleFontSize">比例尺文本大小</param>
        /// <param name="_scale">缩放比例</param>
        /// <param name="tstyle">文字样式</param>
        /// <param name="isModel">是否存入模型空间</param>
        /// <returns>返回DBObjectCollection数据集，1：标题文本；2：上横线；3：下横线；4：比例尺文本</returns>
        public static void AddTitle(Database db,Point2d insertPt,ref Extents2d ext, string text, string scaleString,
             double _scale = 1, double titleFontSize = 4.0, double scaleFontSize = 2.5,string tstyle = "SRB.HT")
        {
            try
            {
                //DBObjectCollection res = new DBObjectCollection();
                //Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    LayerTable   lt=tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                  OpenMode.ForWrite) as BlockTableRecord;
                    DBText Title = new DBText()
                    {
                        Height = titleFontSize * _scale,
                        TextStyleId = st[tstyle],
                        TextString = text,
                        WidthFactor = 0.75,
                        HorizontalMode = TextHorizontalMode.TextCenter,
                        VerticalMode = TextVerticalMode.TextBase,
                        AlignmentPoint = insertPt.C3D(0, 2 * _scale),
                    };

                    double width = Title.GetWidth();
                    Title.Position = insertPt.C3D(-width / 2, Title.GetHeight() + 2 * _scale);
                    modelSpace.AppendEntity(Title);
                    tr.AddNewlyCreatedDBObject(Title, true);
                    ext = ext.Add(new Extents2d(Title.Bounds.Value.MinPoint.C2D(), Title.Bounds.Value.MaxPoint.C2D()));

                    Polyline line = new Polyline() { Closed = false, LayerId = lt["H细线"] };//定义不封闭的Polyline 平面虚线
                    line.AddVertexAt(0, insertPt.C2D(-width / 2 - 1 * _scale, 1 * _scale), 0, 0.5 * _scale, 0.5 * _scale);
                    line.AddVertexAt(1, insertPt.C2D(width / 2 + 1 * _scale, 1 * _scale), 0, 0.5 * _scale, 0.5 * _scale);
                    modelSpace.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D())); ;

                    Polyline line1 = new Polyline() { Closed = false, LayerId = lt["H细线"] };//定义不封闭的Polyline 平面虚线
                    line1.AddVertexAt(0, insertPt.C2D(-width / 2 - 1 * _scale, 0), 0, 0.15 * _scale, 0.15 * _scale);
                    line1.AddVertexAt(1, insertPt.C2D(width / 2 + 1 * _scale, 0), 0, 0.15 * _scale, 0.15 * _scale);
                    modelSpace.AppendEntity(line1);
                    tr.AddNewlyCreatedDBObject(line1, true);
                    ext = ext.Add(new Extents2d(line1.Bounds.Value.MinPoint.C2D(), line1.Bounds.Value.MaxPoint.C2D()));

                    if (!string.IsNullOrEmpty(scaleString))
                    {
                        DBText num = new DBText();
                        num.TextStyleId = st[tstyle];
                        num.TextString = scaleString;
                        num.Height = scaleFontSize * _scale;
                        num.Position = insertPt.C3D(width / 2 + 1.5 * _scale, 1 * _scale);

                        num.HorizontalMode = TextHorizontalMode.TextLeft;
                        num.VerticalMode = TextVerticalMode.TextVerticalMid;
                        num.AlignmentPoint = num.Position;

                        num.WidthFactor = 0.75;
                        //num.WidthFactor = st[tstyle].GetXScale();
                        num.LayerId = lt["H文本"];
                        modelSpace.AppendEntity(num);
                        tr.AddNewlyCreatedDBObject(num, true);
                        ext = ext.Add(new Extents2d(num.Bounds.Value.MinPoint.C2D(), num.Bounds.Value.MaxPoint.C2D()));
                    }

                  
                    tr.Commit();
                }
               
            }
            catch(System.Exception ex)
            {
                //throw new Exception(ex.Message.ToString());
            }
        }

        public static void AddTitleRotate(Database db, Point2d insertPt, ref Extents2d ext, string text, string scaleString,
     double _scale = 1, double titleFontSize = 4.0, double scaleFontSize = 2.5, string tstyle = "SRB.HT")
        {
            try
            {
                //DBObjectCollection res = new DBObjectCollection();
                //Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                  OpenMode.ForWrite) as BlockTableRecord;
                    DBText Title = new DBText()
                    {
                        Height = titleFontSize * _scale,
                        TextStyleId = st[tstyle],
                        TextString = text,
                        WidthFactor = 0.75,
                        HorizontalMode = TextHorizontalMode.TextCenter,
                        VerticalMode = TextVerticalMode.TextBase,
                        AlignmentPoint = insertPt.C3D(-2 * _scale),
                    };

                    double width = Title.GetWidth();
                    Title.Position = insertPt.C3D(Title.GetHeight() + 2 * _scale, -width / 2);
                    Title.Rotation = GeTools.DegreeToRadian(90);
                    modelSpace.AppendEntity(Title);
                    tr.AddNewlyCreatedDBObject(Title, true);
                    ext = ext.Add(new Extents2d(Title.Bounds.Value.MinPoint.C2D(), Title.Bounds.Value.MaxPoint.C2D()));

                    Polyline line = new Polyline() { Closed = false, LayerId = lt["H细线"] };//定义不封闭的Polyline 平面虚线
                    line.AddVertexAt(0, insertPt.C2D(1 * _scale ,- width / 2 - 1 * _scale), 0, 0.5 * _scale, 0.5 * _scale);
                    line.AddVertexAt(1, insertPt.C2D(1 * _scale,width / 2 + 1 * _scale), 0, 0.5 * _scale, 0.5 * _scale);
                    modelSpace.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D())); ;

                    Polyline line1 = new Polyline() { Closed = false, LayerId = lt["H细线"] };//定义不封闭的Polyline 平面虚线
                    line1.AddVertexAt(0, insertPt.C2D(0 ,- width / 2 - 1 * _scale), 0, 0.15 * _scale, 0.15 * _scale);
                    line1.AddVertexAt(1, insertPt.C2D(0, width / 2 + 1 * _scale), 0, 0.15 * _scale, 0.15 * _scale);
                    modelSpace.AppendEntity(line1);
                    tr.AddNewlyCreatedDBObject(line1, true);
                    ext = ext.Add(new Extents2d(line1.Bounds.Value.MinPoint.C2D(), line1.Bounds.Value.MaxPoint.C2D()));

                    if (!string.IsNullOrEmpty(scaleString))
                    {
                        DBText num = new DBText();
                        num.TextStyleId = st[tstyle];
                        num.TextString = scaleString;
                        num.Height = scaleFontSize * _scale;
                        num.Position = insertPt.C3D(1 * _scale,width / 2 + 1.5 * _scale);
                        num.Rotation = GeTools.DegreeToRadian(90);
                        num.HorizontalMode = TextHorizontalMode.TextLeft;
                        num.VerticalMode = TextVerticalMode.TextVerticalMid;
                        num.AlignmentPoint = num.Position;

                        num.WidthFactor = 0.75;
                        //num.WidthFactor = st[tstyle].GetXScale();
                        num.LayerId = lt["H文本"];
                        modelSpace.AppendEntity(num);
                        tr.AddNewlyCreatedDBObject(num, true);
                        ext = ext.Add(new Extents2d(num.Bounds.Value.MinPoint.C2D(), num.Bounds.Value.MaxPoint.C2D()));
                    }


                    tr.Commit();
                }

            }
            catch (System.Exception ex)
            {
                //throw new Exception(ex.Message.ToString());
            }
        }
        public static void AddTitleToPaper(Database db, Point2d insertPt, ref Extents2d ext, string text, string scaleString,
         double _scale = 1, double titleFontSize = 4.0, double scaleFontSize = 2.5, string tstyle = "SRB.HT")
        {
            try
            {
                //DBObjectCollection res = new DBObjectCollection();
                //Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace],
                  OpenMode.ForWrite) as BlockTableRecord;
                    DBText Title = new DBText()
                    {
                        Height = titleFontSize * _scale,
                        TextStyleId = st[tstyle],
                        TextString = text,
                        WidthFactor = 0.75,
                        HorizontalMode = TextHorizontalMode.TextCenter,
                        VerticalMode = TextVerticalMode.TextBase,
                        AlignmentPoint = insertPt.C3D(0, 2 * _scale),
                    };

                    double width = Title.GetWidth();
                    Title.Position = insertPt.C3D(-width / 2, Title.GetHeight() + 2 * _scale);
                    modelSpace.AppendEntity(Title);
                    tr.AddNewlyCreatedDBObject(Title, true);
                    ext = ext.Add(new Extents2d(Title.Bounds.Value.MinPoint.C2D(), Title.Bounds.Value.MaxPoint.C2D()));

                    Polyline line = new Polyline() { Closed = false, LayerId = lt["H细线"] };//定义不封闭的Polyline 平面虚线
                    line.AddVertexAt(0, insertPt.C2D(-width / 2 - 1 * _scale, 1 * _scale), 0, 0.5 * _scale, 0.5 * _scale);
                    line.AddVertexAt(1, insertPt.C2D(width / 2 + 1 * _scale, 1 * _scale), 0, 0.5 * _scale, 0.5 * _scale);
                    modelSpace.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D())); ;

                    Polyline line1 = new Polyline() { Closed = false, LayerId = lt["H细线"] };//定义不封闭的Polyline 平面虚线
                    line1.AddVertexAt(0, insertPt.C2D(-width / 2 - 1 * _scale, 0), 0, 0.15 * _scale, 0.15 * _scale);
                    line1.AddVertexAt(1, insertPt.C2D(width / 2 + 1 * _scale, 0), 0, 0.15 * _scale, 0.15 * _scale);
                    modelSpace.AppendEntity(line1);
                    tr.AddNewlyCreatedDBObject(line1, true);
                    ext = ext.Add(new Extents2d(line1.Bounds.Value.MinPoint.C2D(), line1.Bounds.Value.MaxPoint.C2D()));

                    if (!string.IsNullOrEmpty(scaleString))
                    {
                        DBText num = new DBText();
                        num.TextStyleId = st[tstyle];
                        num.TextString = scaleString;
                        num.Height = scaleFontSize * _scale;
                        num.Position = insertPt.C3D(width / 2 + 1.5 * _scale, 1 * _scale);

                        num.HorizontalMode = TextHorizontalMode.TextLeft;
                        num.VerticalMode = TextVerticalMode.TextVerticalMid;
                        num.AlignmentPoint = num.Position;

                        num.WidthFactor = 0.75;
                        //num.WidthFactor = st[tstyle].GetXScale();
                        num.LayerId = lt["H文本"];
                        modelSpace.AppendEntity(num);
                        tr.AddNewlyCreatedDBObject(num, true);
                        ext = ext.Add(new Extents2d(num.Bounds.Value.MinPoint.C2D(), num.Bounds.Value.MaxPoint.C2D()));
                    }


                    tr.Commit();
                }

            }
            catch (System.Exception ex)
            {
                //throw new Exception(ex.Message.ToString());
            }
        }

        public static void AddTitle(Database db, Point2d insertPt, ref Extents2d ext, string text,
     double _scale = 1, double titleFontSize = 4.0, double scaleFontSize = 2.5, string tstyle = "SRB.HT")
        {
            try
            {
                //DBObjectCollection res = new DBObjectCollection();
                //Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                  OpenMode.ForWrite) as BlockTableRecord;
                    DBText Title = new DBText()
                    {
                        Height = titleFontSize * _scale,
                        TextStyleId = st[tstyle],
                        TextString = text,
                        WidthFactor = 0.75,
                        HorizontalMode = TextHorizontalMode.TextCenter,
                        VerticalMode = TextVerticalMode.TextBase,
                        AlignmentPoint = insertPt.C3D(0, 2 * _scale),
                    };

                    double width = Title.GetWidth();
                    Title.Position = insertPt.C3D(-width / 2, Title.GetHeight() + 2 * _scale);
                    modelSpace.AppendEntity(Title);
                    tr.AddNewlyCreatedDBObject(Title, true);
                    ext = ext.Add(new Extents2d(Title.Bounds.Value.MinPoint.C2D(), Title.Bounds.Value.MaxPoint.C2D()));

                    tr.Commit();
                }

            }
            catch (System.Exception ex)
            {
                //throw new Exception(ex.Message.ToString());
            }
        }
        public static void AddTitleS(Database db, Point2d insertPt, ref Extents2d ext, string text, string textS, string scaleString,
          double _scale = 1, double titleFontSize = 4.0, double scaleFontSize = 2.5, string tstyle = "SRB.HT")
        {
            try
            {
                //DBObjectCollection res = new DBObjectCollection();
                //Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                  OpenMode.ForWrite) as BlockTableRecord;
                    DBText Title = new DBText()
                    {
                        Height = titleFontSize * _scale,
                        TextStyleId = st[tstyle],
                        TextString = text,
                        //WidthFactor = st[tstyle].GetXScale(),
                        HorizontalMode = TextHorizontalMode.TextCenter,
                        VerticalMode = TextVerticalMode.TextBase,
                        AlignmentPoint = insertPt.C3D(0, 6.5 * _scale),
                        LayerId = lt["TEXT(En)"],
                   };

                    DBText TitleS = new DBText()
                    {
                        Height = titleFontSize * _scale,
                        TextStyleId = st[tstyle],
                        TextString = textS,
                        //WidthFactor = st[tstyle].GetXScale(),
                        HorizontalMode = TextHorizontalMode.TextCenter,
                        VerticalMode = TextVerticalMode.TextBase,
                        AlignmentPoint = insertPt.C3D(0, 2 * _scale),
                        LayerId = lt["TEXT(M)"],
                    };
                    double width = Title.GetWidth() > TitleS.GetWidth() ? Title.GetWidth():TitleS.GetWidth();
                    Title.Position = insertPt.C3D(-width / 2, Title.GetHeight() + 2 * _scale);
                    modelSpace.AppendEntity(Title);
                    tr.AddNewlyCreatedDBObject(Title, true);
                    ext = ext.Add(new Extents2d(Title.Bounds.Value.MinPoint.C2D(), Title.Bounds.Value.MaxPoint.C2D()));

                    modelSpace.AppendEntity(TitleS);
                    tr.AddNewlyCreatedDBObject(TitleS, true);
                    ext = ext.Add(new Extents2d(TitleS.Bounds.Value.MinPoint.C2D(), TitleS.Bounds.Value.MaxPoint.C2D()));

                    Polyline line = new Polyline() { Closed = false, LayerId = lt["H细线"] };//定义不封闭的Polyline 平面虚线
                    line.AddVertexAt(0, insertPt.C2D(-width / 2 - 2 * _scale, 1 * _scale), 0, -0.25 * _scale, -0.25 * _scale);
                    line.AddVertexAt(1, insertPt.C2D(width / 2 + 1 * _scale, 1 * _scale), 0, -0.25 * _scale, -0.25 * _scale);
                    modelSpace.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D())); ;

                    //Polyline line1 = new Polyline() { Closed = false, LayerId = lt["H文本"] };//定义不封闭的Polyline 平面虚线
                    //line1.AddVertexAt(0, insertPt.C2D(-width / 2 - 2 * _scale, 0), 0, 0, 0);
                    //line1.AddVertexAt(1, insertPt.C2D(width / 2 + 1 * _scale, 0), 0, 0, 0);
                    //modelSpace.AppendEntity(line1);
                    //tr.AddNewlyCreatedDBObject(line1, true);
                    //ext = ext.Add(new Extents2d(line1.Bounds.Value.MinPoint.C2D(), line1.Bounds.Value.MaxPoint.C2D()));

                    if (!string.IsNullOrEmpty(scaleString))
                    {
                        DBText num = new DBText();
                        num.TextStyleId = st[tstyle];
                        num.TextString = scaleString;
                        num.Height = scaleFontSize * _scale;
                        num.Position = insertPt.C3D(0,-2 * _scale);

                        num.HorizontalMode = TextHorizontalMode.TextLeft;
                        num.VerticalMode = TextVerticalMode.TextVerticalMid;
                        num.AlignmentPoint = num.Position;


                        //num.WidthFactor = st[tstyle].GetXScale();
                        num.LayerId = lt["H文本"];
                        modelSpace.AppendEntity(num);
                        tr.AddNewlyCreatedDBObject(num, true);
                        ext = ext.Add(new Extents2d(num.Bounds.Value.MinPoint.C2D(), num.Bounds.Value.MaxPoint.C2D()));
                    }


                    tr.Commit();
                }

            }
            catch (System.Exception ex)
            {
                //throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 绘制文字(DB)
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="Point"></param>
        /// <param name="text"></param>
        /// <param name="_scale"></param>
        /// <param name="height"></param>
        /// <param name="tstyle"></param>
        /// <param name="Rot_rad"></param>
        /// <returns></returns>
        public static void AddDBText(Database db, Point3d insertPt,ref Extents2d ext, string text, double _scale,
            double height=2.5, string tstyle = "SRB.HT", double Rot_rad = 0,
            TextHorizontalMode hm=TextHorizontalMode.TextLeft,TextVerticalMode vm=TextVerticalMode.TextBottom,string layer="H文本")
        {
            try
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                //DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                 OpenMode.ForWrite) as BlockTableRecord;
                    double WidthFactor = 0.75;
                    double.TryParse(IniOper.getInstance().FontWidth,out WidthFactor);
                    //DBText
                    DBText txt = new DBText() {WidthFactor= WidthFactor };
                    txt.Height = height * _scale;
                    txt.TextString = text;
                    txt.TextStyleId = st[tstyle];
                   if(tstyle == "SRB.tssdeng")
                    {
                        txt.WidthFactor = 0.85;
                    }
                    txt.Position = insertPt;
                    txt.Rotation = Rot_rad;
                    //txt.WidthFactor = st[tstyle].GetXScale();
                    txt.LayerId = lt[layer];
                    txt.HorizontalMode = hm;
                    txt.VerticalMode = vm;
                    txt.AlignmentPoint = insertPt;

                    modelSpace.AppendEntity(txt);
                    tr.AddNewlyCreatedDBObject(txt, true);
                    ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.C2D(), txt.Bounds.Value.MaxPoint.C2D()));
                    tr.Commit();
                }
            }
            catch(System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        public static void AddCirleDBText(Database db, Point3d insertPt, ref Extents2d ext, string textC, double _scale,
double height = 2.5, double Rot_rad = 0, string tstyle = "SRB.HT", string layer = "H文本")
        {
            try
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                //DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                 OpenMode.ForWrite) as BlockTableRecord;

                    DBText txtC = new DBText();
                    txtC.TextString = textC.ToString();
                    txtC.Height = height * _scale;
                    txtC.Position = insertPt;
                    txtC.HorizontalMode = TextHorizontalMode.TextMid;
                    txtC.VerticalMode = TextVerticalMode.TextVerticalMid;
                    txtC.AlignmentPoint = insertPt;
                    txtC.Rotation = Rot_rad;
                    txtC.TextStyleId = st[tstyle];
                    txtC.LayerId = lt[layer];
                    txtC.WidthFactor = 0.75;
                    Circle C2 = new Circle();
                    C2 = new Circle(insertPt.C3D(), Vector3d.ZAxis, (height + 2) * 0.5 * _scale);
                    C2.LayerId = lt[layer];


                    modelSpace.AppendEntity(txtC);
                    tr.AddNewlyCreatedDBObject(txtC, true);

                    modelSpace.AppendEntity(C2);
                    tr.AddNewlyCreatedDBObject(C2, true);
                    ext = ext.Add(new Extents2d(C2.Bounds.Value.MinPoint.C2D(), C2.Bounds.Value.MaxPoint.C2D()));
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }


        public static void AddTexWithArrow(Database db, Point3d insertPt, ref Extents2d ext, string text, double _scale,
         double height = 2.5,eDirection dir=eDirection.E_TOP, double width = 0,
            TextHorizontalMode hm = TextHorizontalMode.TextLeft, TextVerticalMode vm = TextVerticalMode.TextBottom,  string tstyle = "SRB.HT",string layer = "H文本")
        {
            try
            {
                double w = 0, h = (text.Length * 1.5) * 3 * _scale;
                //Database db = HostApplicationServices.WorkingDatabase;
                MText txt1 = TextPloter.GetTextActualWidth(db, text, _scale, out w, out h, height, Extensions.curFont);
                if (h > w)
                    w = h;
                //DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                 OpenMode.ForWrite) as BlockTableRecord;
                    double WidthFactor = 0.75;
                    double.TryParse(IniOper.getInstance().FontWidth, out WidthFactor);
                    //MText
                    MText Mtext = new MText();
                    Mtext.TextHeight = height * _scale;
                    Mtext.Contents = text;
                    Mtext.TextStyleId = st[tstyle];
                    Mtext.Width = width * _scale;

                      Solid Array;
                    Line L;
                    switch (dir)
                    {
                        case eDirection.E_TOP:
                            w = w * 1.1;
                     
                            Array = new Solid(
                             insertPt.C3D(0, w),
                             insertPt.C3D(0.6 * _scale, w + 0.6 * _scale),
                             insertPt.C3D(0, w + 2.5 * _scale)
                             )
                            { LayerId = lt["H标注"] };


                            modelSpace.AppendEntity(Array);
                            tr.AddNewlyCreatedDBObject(Array, true);

                            L = new Line(insertPt.C3D(), insertPt.C3D(0, w)) { LayerId = lt["H标注"] };
                            modelSpace.AppendEntity(L);
                            tr.AddNewlyCreatedDBObject(L, true);

                            Mtext.Location = insertPt.C3D(1 * _scale, w);
                            break;
                        case eDirection.E_BOTTOM:
                            Array = new Solid(
                         insertPt.C3D(0, -w),
                         insertPt.C3D(0.6 * _scale, -(w + 0.6 * _scale)),
                         insertPt.C3D(0, -(w + 2.5 * _scale))
                         )
                            { LayerId = lt["H标注"] };


                            modelSpace.AppendEntity(Array);
                            tr.AddNewlyCreatedDBObject(Array, true);

                            L = new Line(insertPt, insertPt.C3D(0, -w)) { LayerId = lt["H标注"] };
                            modelSpace.AppendEntity(L);
                            tr.AddNewlyCreatedDBObject(L, true);

                            Mtext.Location = insertPt.C3D(1 * _scale);
                            break;
                        case eDirection.E_RIGHT:
                            w = w * 1.1;
                            Array = new Solid(
                         insertPt.C3D(w),
                         insertPt.C3D((w + 0.6 * _scale), 0.6 * _scale),
                         insertPt.C3D((w + 2.5 * _scale))
                         )
                            { LayerId = lt["H标注"] };


                            modelSpace.AppendEntity(Array);
                            tr.AddNewlyCreatedDBObject(Array, true);

                            L = new Line(insertPt, insertPt.C3D(w)) { LayerId = lt["H标注"] };
                            modelSpace.AppendEntity(L);
                            tr.AddNewlyCreatedDBObject(L, true);
                            Mtext.Location = insertPt.C3D(0.5 * _scale, (height+1) * _scale);
                            break;
                        case eDirection.E_LEFT:
                            w = w * 1.1;
                            Array = new Solid(
                         insertPt.C3D(-w),
                         insertPt.C3D( -(w + 0.6 * _scale), 0.6 * _scale),
                         insertPt.C3D(-(w + 2.5 * _scale))
                         )
                            { LayerId = lt["H标注"] };


                            modelSpace.AppendEntity(Array);
                            tr.AddNewlyCreatedDBObject(Array, true);

                            L = new Line(insertPt, insertPt.C3D( -w)) { LayerId = lt["H标注"] };
                            modelSpace.AppendEntity(L);
                            tr.AddNewlyCreatedDBObject(L, true);

                            Mtext.Location = insertPt.C3D(-w-0.2* _scale, (height + 1) * _scale);
                            break;
                    }


                    Mtext.LayerId = lt["H文本"];
                    Mtext.LineSpaceDistance = (height * 1.3) * _scale;
                    modelSpace.AppendEntity(Mtext);
                    tr.AddNewlyCreatedDBObject(Mtext, true);
 
                    ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        public static void AddPaperDBText(Database db, Point3d insertPt, ref Extents2d ext, string text, double _scale,
           double height = 2.5, string tstyle = "SRB.HT", double Rot_rad = 0,
           TextHorizontalMode hm = TextHorizontalMode.TextLeft, TextVerticalMode vm = TextVerticalMode.TextBottom, string layer = "H文本")
        {
            try
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                //DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace],
                 OpenMode.ForWrite) as BlockTableRecord;
                    double WidthFactor = 0.7;
                    double.TryParse(IniOper.getInstance().FontWidth, out WidthFactor);
                    //DBText
                    DBText txt = new DBText() { WidthFactor = WidthFactor };
                    txt.Height = height * _scale;
                    txt.TextString = text;
                    txt.TextStyleId = st[tstyle];
                  
                    txt.Position = insertPt;
                    txt.Rotation = Rot_rad;
                    //txt.WidthFactor = st[tstyle].GetXScale();
                    txt.LayerId = lt[layer];
                    txt.HorizontalMode = hm;
                    txt.VerticalMode = vm;
                    txt.AlignmentPoint = insertPt;
                    txt.WidthFactor = WidthFactor;
                    modelSpace.AppendEntity(txt);
                    tr.AddNewlyCreatedDBObject(txt, true);
                    ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.C2D(), txt.Bounds.Value.MaxPoint.C2D()));
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        public static void AddDBTextWithCirle(Database db, Point3d insertPt, ref Extents2d ext, string text, string textC, double _scale,
     double height = 2.5,double Rot_rad = 0, string tstyle = "SRB.HT",  string layer = "H文本")
        {
            try
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                //DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                 OpenMode.ForWrite) as BlockTableRecord;

                    DBText txtC = new DBText();
                    txtC.TextString = textC.ToString();
                    txtC.Height = height * _scale;
                    txtC.Position = insertPt;
                    txtC.HorizontalMode = TextHorizontalMode.TextMid;
                    txtC.VerticalMode = TextVerticalMode.TextVerticalMid;
                    txtC.AlignmentPoint = insertPt;
                    txtC.Rotation = Rot_rad;
                    txtC.TextStyleId = st[tstyle];
                    txtC.LayerId = lt[layer];
                    txtC.WidthFactor = 0.8;
                    Circle C2 = new Circle();
                    C2 = new Circle(insertPt.C3D(), Vector3d.ZAxis, (height + 0.5)*0.5 * _scale);
                    C2.LayerId = lt[layer];

                    DBText txt = new DBText();
                    txt.Height = height * _scale;
                    txt.TextString = text;
                    txt.TextStyleId = st[tstyle];
                    txt.Position = insertPt.C3D((height + 0.5) * 0.5 * _scale*Math.Cos(Rot_rad), (height + 0.5) * 0.5 * _scale * Math.Sin(Rot_rad));
                    txt.Rotation = Rot_rad;
                    //txt.WidthFactor = st[tstyle].GetXScale();
                    txt.LayerId = lt[layer];
                    txt.HorizontalMode = TextHorizontalMode.TextLeft;
                    txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                    txt.AlignmentPoint = insertPt.C3D((height + 0.5) * 0.5 * _scale * Math.Cos(Rot_rad), (height + 0.5) * 0.5 * _scale * Math.Sin(Rot_rad)); ;
                    modelSpace.AppendEntity(txtC);
                    tr.AddNewlyCreatedDBObject(txtC, true);
                    modelSpace.AppendEntity(txt);
                    tr.AddNewlyCreatedDBObject(txt, true);
                    modelSpace.AppendEntity(C2);
                    tr.AddNewlyCreatedDBObject(C2, true);
                    ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.C2D(), txt.Bounds.Value.MaxPoint.C2D()));
                    ext = ext.Add(new Extents2d(C2.Bounds.Value.MinPoint.C2D(), C2.Bounds.Value.MaxPoint.C2D()));
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        public static void AddDBTextWithSCirle(Database db, Point3d insertPt, ref Extents2d ext, string text, string textC, double _scale,out double txtlen,
   double Rot_rad = 0, double height = 2.5, string tstyle = "SRB.HT", string layer = "H文本")
        {
            try
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                double w1 = 0, h1 = 0;
                MText txt1 = TextPloter.GetTextActualWidth(db, text, _scale, out w1, out h1, 2.5, Extensions.curFont);
                txtlen = w1 + (height  + 0.8) * 0.5 * _scale+5*_scale;
                //DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                 OpenMode.ForWrite) as BlockTableRecord;

                    DBText txtC = new DBText();
                    txtC.TextString = textC.ToString();
                    txtC.Height = height * _scale;
                    txtC.Position = insertPt;
                    txtC.HorizontalMode = TextHorizontalMode.TextMid;
                    txtC.VerticalMode = TextVerticalMode.TextVerticalMid;
                    txtC.AlignmentPoint = insertPt;
                    txtC.Rotation = Rot_rad;
                    txtC.TextStyleId = st[tstyle];
                    txtC.LayerId = lt[layer];
                    txtC.WidthFactor = 0.8;
                    Circle C2 = new Circle();
                    C2 = new Circle(insertPt.C3D(), Vector3d.ZAxis, (height + 0.8) * 0.5 * _scale);
                    C2.LayerId = lt[layer];

                    DBText txt = new DBText();
                    txt.Height = height * _scale;
                    txt.TextString = text;
                    txt.TextStyleId = st[tstyle];
                    txt.Position = insertPt.C3D((height + 0.8) * 0.5 * _scale * Math.Cos(Rot_rad), (height + 0.8) * 0.5 * _scale * Math.Sin(Rot_rad));
                    txt.Rotation = Rot_rad;
                    //txt.WidthFactor = st[tstyle].GetXScale();
                    txt.LayerId = lt[layer];
                    txt.HorizontalMode = TextHorizontalMode.TextLeft;
                    txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                    txt.AlignmentPoint = insertPt.C3D((height + 0.8) * 0.5 * _scale * Math.Cos(Rot_rad), (height + 0.8) * 0.5 * _scale * Math.Sin(Rot_rad)); ;
                    modelSpace.AppendEntity(txtC);
                    tr.AddNewlyCreatedDBObject(txtC, true);
                    modelSpace.AppendEntity(txt);
                    tr.AddNewlyCreatedDBObject(txt, true);
                    modelSpace.AppendEntity(C2);
                    tr.AddNewlyCreatedDBObject(C2, true);
                    ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.C2D(), txt.Bounds.Value.MaxPoint.C2D()));
                    ext = ext.Add(new Extents2d(C2.Bounds.Value.MinPoint.C2D(), C2.Bounds.Value.MaxPoint.C2D()));
                 
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }

         
        }
        public static void AddDBTextWithListCirle(Database db, Point3d insertPt, ref Extents2d ext, string text, List<string> textC, double _scale,
double height = 2.5, double Rot_rad = 0, string tstyle = "SRB.HT", string layer = "H文本")
        {
            try
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                //DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                 OpenMode.ForWrite) as BlockTableRecord;

                    for (int i = 0; i < textC.Count; i++)
                    {
                        DBText txtC = new DBText();
                        txtC.TextString = textC[i];
                        txtC.Height = height * _scale;
                        txtC.Position = insertPt.C3D((height + 0.5)  * _scale * i * Math.Cos(Rot_rad), (height + 0.5)  * _scale * i * Math.Sin(Rot_rad));
                        txtC.HorizontalMode = TextHorizontalMode.TextMid;
                        txtC.VerticalMode = TextVerticalMode.TextVerticalMid;
                        txtC.AlignmentPoint = insertPt.C3D((height + 0.5)  * _scale * i  * Math.Cos(Rot_rad), (height + 0.5) * _scale * i  * Math.Sin(Rot_rad));
                        txtC.Rotation = Rot_rad;
                        txtC.TextStyleId = st[tstyle];
                        txtC.LayerId = lt[layer];
                        txtC.WidthFactor = 0.8;
                        Circle C2 = new Circle();
                        C2 = new Circle(insertPt.C3D((height + 0.5)  * _scale * i * Math.Cos(Rot_rad), (height + 0.5)  * _scale * i * Math.Sin(Rot_rad)), Vector3d.ZAxis, (height + 0.5) * 0.5 * _scale);
                        C2.LayerId = lt[layer];
                        modelSpace.AppendEntity(txtC);
                        tr.AddNewlyCreatedDBObject(txtC, true);
                        modelSpace.AppendEntity(C2);
                        tr.AddNewlyCreatedDBObject(C2, true);
                        ext = ext.Add(new Extents2d(C2.Bounds.Value.MinPoint.C2D(), C2.Bounds.Value.MaxPoint.C2D()));
                    }

                    DBText txt = new DBText();
                    txt.Height = height * _scale;
                    txt.TextString = text;
                    txt.TextStyleId = st[tstyle];
                    txt.Position = insertPt.C3D((height + 0.5)  * _scale * Math.Cos(Rot_rad)* (textC.Count-0.5), (height + 0.5)  * _scale * Math.Sin(Rot_rad) * (textC.Count - 0.5));
                    txt.Rotation = Rot_rad;
                    //txt.WidthFactor = st[tstyle].GetXScale();
                    txt.LayerId = lt[layer];
                    txt.HorizontalMode = TextHorizontalMode.TextLeft;
                    txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                    txt.AlignmentPoint = insertPt.C3D((height + 0.5) * _scale * Math.Cos(Rot_rad) * (textC.Count - 0.5), (height + 0.5) * _scale * Math.Sin(Rot_rad) * (textC.Count - 0.5)); ;
               
                    modelSpace.AppendEntity(txt);
                    tr.AddNewlyCreatedDBObject(txt, true);     
                    ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.C2D(), txt.Bounds.Value.MaxPoint.C2D()));
                  
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 从文件读取设计说明，生成多行文本，左上角点插入。
        /// </summary>
        /// <param name="LeftUpRefPoint"></param>
        /// <param name="FilePath"></param>
        /// <param name="width">文本宽度</param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static void AddNoteFromFile(Database db, Point3d LeftUpRefPoint, ref Extents2d ext, string FilePath, double width, double height = 3.5, string tstyle = "SRB.HT")
        {
            //DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            List<string> content=new List<string>();
            using (StreamReader sr = new StreamReader(FilePath))
            {
                string line;             
                while ((line = sr.ReadLine()) != null)
                {
                    content.Add(line.Trim());
                }
            }
            var text="\\pxi-1,l1,qj;注：\\P"+string.Join("\\P", content);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                MText Mtext = new MText()
                {
                    TextHeight = height,
                    Contents = text,
                    TextStyleId = st[tstyle],
                    Location = LeftUpRefPoint,
                    LayerId = lt["H文本"],
                    LineSpacingFactor = 1.0,
                    LineSpacingStyle = LineSpacingStyle.Exactly,
                    Width = width,
                    
                };

                modelSpace.AppendEntity(Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);
                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }          
        }

        /// <summary>
        /// 绘制多行文字
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="Point"></param>
        /// <param name="text"></param>
        /// <param name="_scale"></param>
        /// <param name="height">字体高度</param>
        /// <param name="strWidth">字符串宽度-1：不限宽度 ；>0：多行文本字符串宽度不能超过设置值</param>
        /// <param name="tstyle"></param>
        /// <param name="Rotation"></param>
        /// <returns></returns>
        public static void AddMText(Database db, Point3d insertPt,ref Extents2d ext, string text, double _scale,double height = 2, 
            double strWidth = -1, string tstyle = "SRB.HT", double Rotation = 0)
        {
            try
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                //DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
               OpenMode.ForWrite) as BlockTableRecord;
                    MText Mtext = new MText();
                    Mtext.TextHeight = height * _scale;
                    Mtext.Contents = text;
                    Mtext.TextStyleId = st[tstyle];
                    Mtext.Location = insertPt;
                    Mtext.LayerId = lt["H文本"];
                    Mtext.Rotation = Rotation;
                    Mtext.LineSpaceDistance = (height*1.5) * _scale;
        
                    if (strWidth > 0)
                    {
                        Mtext.Width = strWidth;
                    }

                    modelSpace.AppendEntity(Mtext);
                    tr.AddNewlyCreatedDBObject(Mtext, true);
                    ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                    tr.Commit();
                }
            }
            catch(System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        public static void AddMText2Paper(Database db, Point3d insertPt, ref Extents2d ext, string text, double _scale, double height = 2,
          double strWidth = -1, string tstyle = "SRB.HT", double Rotation = 0)
        {
            try
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                //DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace],
               OpenMode.ForWrite) as BlockTableRecord;
                    MText Mtext = new MText();
                    Mtext.TextHeight = height * _scale;
                    Mtext.Contents = text;
                    Mtext.TextStyleId = st[tstyle];
                    Mtext.Location = insertPt;
                    Mtext.LayerId = lt["H文本"];
                    Mtext.Rotation = Rotation;
                    Mtext.LineSpaceDistance = (height + 1) * _scale;

                    if (strWidth > 0)
                    {
                        Mtext.Width = strWidth;
                    }

                    modelSpace.AppendEntity(Mtext);
                    tr.AddNewlyCreatedDBObject(Mtext, true);
                    ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 绘制带下划线的文本
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="Point"></param>
        /// <param name="text"></param>
        /// <param name="_scale"></param>
        /// <param name="height"></param>
        /// <param name="tstyle"></param>
        /// <param name="Rotation"></param>
        /// <returns>返回DBObjectCollection数据集，1：标题文本；2：横线</returns>
        public static void AddTitleUnderline(Database db,Point2d insertPt,ref Extents2d ext, string text,double _scale, 
            double height = 2, string tstyle = "SRB.HT", double Rotation = 0)
        {
            try
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                //DBObjectCollection res = new DBObjectCollection();
            
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
               OpenMode.ForWrite) as BlockTableRecord;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    DBText Title = new DBText()
                    {
                        Height = height * _scale,
                        TextStyleId = st[tstyle],
                        TextString = text,
                        WidthFactor = st[tstyle].GetXScale(),
                        HorizontalMode = TextHorizontalMode.TextCenter,
                        VerticalMode = TextVerticalMode.TextBase,
                        AlignmentPoint = insertPt.C3D(0, 2 * _scale),
                    };

                    double width = Title.GetWidth();
                    Title.Position = insertPt.C3D(-width / 2, Title.GetHeight() + 2 * _scale);

                    Polyline line1 = new Polyline() { Closed = false, LayerId = lt["H文本"] };//定义不封闭的Polyline 平面虚线
                    line1.AddVertexAt(0, insertPt.C2D(-width / 2, 0), 0, 0, 0);
                    line1.AddVertexAt(1, insertPt.C2D(width / 2, 0), 0, 0, 0);

                  
                    modelSpace.AppendEntity(Title);
                    tr.AddNewlyCreatedDBObject(Title, true);
                    ext = ext.Add(new Extents2d(Title.Bounds.Value.MinPoint.C2D(), Title.Bounds.Value.MaxPoint.C2D()));
                    modelSpace.AppendEntity(line1);
                    tr.AddNewlyCreatedDBObject(line1, true);
                    ext = ext.Add(new Extents2d(line1.Bounds.Value.MinPoint.C2D(), line1.Bounds.Value.MaxPoint.C2D()));
                    tr.Commit();
                }
               
            }
            catch(System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        public static MText GetTextActualWidth(Database db, string text, double scale,out double txtwidth, out double txtheight,
         double fontSize = 2.5, string tstyle = "SRB.HT")
        {
            tstyle = "SRB.HT";
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
         
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
           OpenMode.ForWrite) as BlockTableRecord;

                //DBText
                MText Mtext = new MText();
                Mtext.TextHeight = fontSize * scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                Mtext.LineSpacingFactor = 1;
                modelSpace.AppendEntity(Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);
                //Mtext.Width = 200 * _scale;
                txtwidth = Mtext.ActualWidth;
                txtheight = Mtext.ActualHeight;
            
                return Mtext;
                //tr.Commit();
            }
        }
    }
}

